package com.coda.controller;

import com.coda.common.PageResult;
import com.coda.common.Result;
import com.coda.dto.PageDto;
import com.coda.entity.*;
import com.coda.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/warehouse/product")
@Api(tags = "商品管理相关接口")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SupplyService supplyService;
    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private PlaceService placeService;
    @Autowired
    private UnitService unitService;
    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询商品列表
     */
    @GetMapping("/product-page-list")
    @ApiOperation("分页查询商品列表")
    public Result productPage(PageDto pageDto, Product product) {
        PageResult pageResult = productService.productPage(pageDto, product);
        return Result.ok(pageResult);
    }

    /**
     * 查询所有仓库
     */
    @GetMapping("/store-list")
    @ApiOperation("查询所有仓库列表")
    public Result getStoreList() {
        List<Store> storeList = storeService.getStoreList();
        return Result.ok(storeList);
    }

    /**
     * 查询所有品牌
     */
    @GetMapping("/brand-list")
    @ApiOperation("查询所有品牌")
    public Result getBrandList() {
        List<Brand> brandList = brandService.getBrandList();
        return Result.ok(brandList);
    }

    /**
     * 查询所有供应商
     */
    @GetMapping("/supply-list")
    @ApiOperation("查询所有供应商")
    public Result getSupplyList() {
        List<Supply> supplyList = supplyService.getSupplyList();
        return Result.ok(supplyList);
    }

    /**
     * 查询所有分类树
     */
    @GetMapping("/category-tree")
    @ApiOperation("查询所有分类树")
    public Result getCategoryList() {
        List<ProductType> productTypeList = productTypeService.getProductTypeList();
        return Result.ok(productTypeList);
    }

    /**
     * 查询所有地区
     */
    @GetMapping("/place-list")
    @ApiOperation("查询所有地区")
    public Result getPlaceList() {
        List<Place> placeList = placeService.getPlaceList();
        return Result.ok(placeList);
    }

    /**
     * 查询所有单位
     */
    @GetMapping("/unit-list")
    @ApiOperation("查询所有单位")
    public Result getUnitList() {
        List<Unit> unitList = unitService.getUnitList();
        return Result.ok(unitList);
    }

    /**
     * 商品图片上传
     */
    @CrossOrigin
    @PostMapping("/img-upload")
    @ApiOperation("商品图片上传")
    public Result uploadImage(MultipartFile file){
        String url = uploadService.uploadImage(file);
        return Result.ok(url);
    }
    /**
     * 添加商品
     */
    @PostMapping("/product-add")
    @ApiOperation("添加商品")
    public Result addProduct(@RequestBody Product product){
        productService.addProduct(product);
        return Result.ok("添加成功");
    }
    /**
     * 修改商品上架下架状态
     */
    @PutMapping("/state-change")
    @ApiOperation("修改商品上架下架状态")
    public Result updateState(@RequestBody Product product){
        productService.updateState(product);
        return Result.ok("修改成功");
    }
    /**
     * 根据id删除单个商品
     */
    @DeleteMapping("/product-delete/{id}")
    @ApiOperation("根据id删除单个商品")
    public Result deleteProduct(@PathVariable Long id){
        productService.deleteProductById(id);
        return Result.ok("删除成功");
    }
    /**
     * 根据id批量删除商品
     */
    @DeleteMapping("/product-list-delete")
    @ApiOperation("根据id批量删除商品")
    public Result deleteProducts(@RequestBody List<Long> ids){
        productService.deleteProductsByIds(ids);
        return Result.ok("删除成功");
    }

    /**
     * 根据id修改商品
     */
    @PutMapping("/product-update")
    @ApiOperation("根据id修改商品")
    public Result update(@RequestBody Product product){
        productService.update(product);
        return Result.ok("修改成功");
    }




}
